package org.xlin.biz.util;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.UnsupportedEncodingException;

public class ResponseUtil {

	static HttpHeaders TEXT_PLAN_HEADER = new HttpHeaders();
	static HttpHeaders JSON_HEADER = new HttpHeaders();

	static {
		TEXT_PLAN_HEADER.setContentType(MediaType.TEXT_PLAIN);
		JSON_HEADER.setContentType(MediaType.APPLICATION_JSON);
	}

	public static ResponseEntity<?> text(String body, HttpStatus status) {
		String ec = body;
		try {
			ec = new String(body.getBytes("UTF-8"),"iso-8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ResponseEntity<String>(ec, TEXT_PLAN_HEADER, status);
	}
	
	public static <T> ResponseEntity<T> json(T body, HttpStatus status) {
		return new ResponseEntity<T>(body, JSON_HEADER, status);
	}
	
	public static <T> ResponseEntity<T> json(T body) {
		return json(body, HttpStatus.OK);
	}
	
	public static ResponseEntity<Void> text(HttpStatus status) {
		return new ResponseEntity<Void>( TEXT_PLAN_HEADER, status);
	}
	
	public static ResponseEntity<Void> ok() {
		return new ResponseEntity<Void>( TEXT_PLAN_HEADER, HttpStatus.OK);
	}
}
